package com.lr;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lr.mapper.BlogDao;
import com.lr.mapper.UserInfoMapper;
import com.lr.po.Blog;
import com.lr.po.UserInfo;
import com.lr.po.UserInfoAndBlogListPO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Java一对一辅导，手把手带你拿高薪！
 * @WeChat：liuliang70
 * @date 2022/1/8 14:38
 */
public class Application {
    public static void main(String[] args) throws Exception {

        extracted();
    }

    // 查询每个同学发表的博客
    private static void extracted() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
        List<UserInfo> userInfos = userInfoMapper.selectAllUsers();
        Map<Long, String> userInfoMap = userInfos
                .stream().
                collect(Collectors.toMap(UserInfo::getUserId, UserInfo::getUsername));
        BlogDao blogDao = sqlSession.getMapper(BlogDao.class);
        List<Long> userIdList = userInfos.stream().map(UserInfo::getUserId).collect(Collectors.toList());
        List<Blog> blogs = blogDao.selectBlogsByIds(userIdList);
        Map<Long, List<Blog>> blogMap = blogs.stream().collect(Collectors.groupingBy(Blog::getUserId));

        List<UserInfoAndBlogListPO> userInfoAndBlogListPOResultList = new ArrayList<>();
        userIdList.forEach(e->{
            UserInfoAndBlogListPO userInfoAndBlogListPO = new UserInfoAndBlogListPO();
            userInfoAndBlogListPO.setUserId(e);
            userInfoAndBlogListPO.setUsername(userInfoMap.get(e));
            userInfoAndBlogListPO.setBlogList(blogMap.get(e));
            userInfoAndBlogListPOResultList.add(userInfoAndBlogListPO);
        });

        userInfoAndBlogListPOResultList.forEach(e -> System.out.println("userInfoAndBlog:" + e));
    }

    private static void queryUserInfoAndBlogList() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        List<UserInfoAndBlogListPO> userInfoAndBlogListPOS = mapper.queryUserInfoAndBlogList();
        userInfoAndBlogListPOS.forEach(e -> System.out.println("userInfoAndBlog:" + e));
    }

    private static void queryUserInfoAndBlogListByUserId() throws IOException {
        //某个人发了什么博客，userid，username,blogTitle,blogContent
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        System.out.println(mapper.queryUserInfoAndBlogListByUserId(1L));
    }

    private static void queryById() throws IOException {
        SqlSession sqlSession = getSqlSession();
        BlogDao mapper = sqlSession.getMapper(BlogDao.class);
        System.out.println(mapper.queryById(1L));
    }

    private static void queryUserInfoByParam1() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("tom");

        PageHelper.startPage(1, 10);
        List<UserInfo> userInfos = mapper.queryUserInfoByParam(userInfo);

        PageInfo<UserInfo> userInfoPageInfo = new PageInfo<>(userInfos);
        long total = userInfoPageInfo.getTotal();

        List<UserInfo> list = userInfoPageInfo.getList();
        System.out.println("总页数:" + total);
        list.forEach(e -> System.out.println("userInfo:" + e));
    }

    private static void selectUserInfoByPages(Integer pageIndex, Integer pageSize) throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        List<UserInfo> userInfos = mapper.selectUserInfoByPages((pageIndex-1)*pageSize, pageSize);
        userInfos.forEach(e -> System.out.println("userInfo:" + e));
        System.out.println("总页数:" + mapper.userInfoCounts());
        sqlSession.close();
    }

    private static void selectUserInfoByIds() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        List<UserInfo> userInfos = mapper.selectUserInfoByIds(Arrays.asList(1L, 2L,1L, 2L));
        userInfos.forEach(e -> System.out.println("userInfo:" + e));
        sqlSession.close();
    }

    private static void updateUserInfoByParam() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("tom3");
        userInfo.setUserPassword("sfd");
        userInfo.setUserId(2L);
        mapper.updateUserInfoByParam(userInfo);
        sqlSession.commit();
        sqlSession.close();
    }

    private static void queryUserInfoByParam() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);

        UserInfo userInfo = new UserInfo();

        //ctrl+alt+v
        List<UserInfo> userInfos = mapper.queryUserInfoByParam(userInfo).stream()
                .filter(e-> !Objects.equals(e.getUserPassword(),"sfd"))
                .sorted(Comparator.comparing(UserInfo::getUpdateTime))
                .collect(Collectors.toList());
        //userInfos.forEach(e -> System.out.println("userInfo:" + e));

        Map<String, List<UserInfo>> map = mapper.queryUserInfoByParam(userInfo)
                .stream()
                .collect(Collectors.groupingBy(UserInfo::getUsername));
        map.forEach((k,v)->{
            System.out.println("name:" + k + ",value:" + v);
        });

        //
        sqlSession.close();
    }

    private static void queryUserInfoByUsernameAndPwd() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo = mapper.queryUserInfoByUsernameAndPwd("'tom' or 1=1 ", "'roo123213321'");
        System.out.println(userInfo);
    }

    private static void deleteUserById() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        mapper.deleteUserById(5L);
        sqlSession.commit();
        sqlSession.close();
    }

    private static void insertUserInfo() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("lsx");
        userInfo.setUserPassword("xxx");
        Integer integer = mapper.insertUserInfo(userInfo);
        System.out.println("结果：" + integer);
        sqlSession.commit();
        sqlSession.close();
    }

    private static void updateUsernameById() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        mapper.updateUsernameById("zs1", 3L);
        sqlSession.commit();
        sqlSession.close();
    }

    private static void getAllUserInfo() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        List<UserInfo> userInfos = mapper.selectAllUsers();
        userInfos.forEach(e -> System.out.println("userInfo:" + e));
        //抽取方法的快捷 ctrl+alt+m
        sqlSession.close();
    }

    private static void getMysqlSimple() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserInfo user = sqlSession.selectOne("selectUser", 1);
        System.out.println(user);
        sqlSession.close();
    }


    private static SqlSession getSqlSession() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory.openSession();
    }
}
